package presentacion;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;

import javax.swing.ComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import negocio.TransferGeneral;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.Component;

public class GUIGeneralNuevoCorreo extends JDialog {

	
	JComboBox comboBoxPara;
	JComboBox comboBox;
	JTextField textPane ;
	JTextPane textPaneContenido;
	ArrayList<Integer> usuarioId;
	private int dniUsuario;
	
	private final JPanel contentPanel = new JPanel();



	/**
	 * Create the dialog.
	 */
	public GUIGeneralNuevoCorreo(int dniUs) {
		this.dniUsuario= dniUs;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{54, 6, 0, 0};
		gbl_contentPanel.rowHeights = new int[]{20, 0, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JLabel lblPara = new JLabel("Para:");
			GridBagConstraints gbc_lblPara = new GridBagConstraints();
			gbc_lblPara.anchor = GridBagConstraints.EAST;
			gbc_lblPara.insets = new Insets(0, 0, 5, 5);
			gbc_lblPara.gridx = 0;
			gbc_lblPara.gridy = 0;
			contentPanel.add(lblPara, gbc_lblPara);
		}
		{
			comboBoxPara = new JComboBox();
			comboBoxPara.setEditable(true);
			GridBagConstraints gbc_comboBoxPara = new GridBagConstraints();
			gbc_comboBoxPara.insets = new Insets(0, 0, 5, 5);
			gbc_comboBoxPara.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBoxPara.gridx = 1;
			gbc_comboBoxPara.gridy = 0;
			contentPanel.add(comboBoxPara, gbc_comboBoxPara);
		}
		{
			comboBox = new JComboBox();
			comboBox.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					actualizarUsuarios();
					
				}
			});
			comboBox.setModel(new DefaultComboBoxModel(Roles.values()));
			GridBagConstraints gbc_comboBox = new GridBagConstraints();
			gbc_comboBox.insets = new Insets(0, 0, 5, 0);
			gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
			gbc_comboBox.gridx = 2;
			gbc_comboBox.gridy = 0;
			contentPanel.add(comboBox, gbc_comboBox);
		}
		{
			JLabel lblAsunto = new JLabel("Asunto:");
			lblAsunto.setAlignmentX(Component.CENTER_ALIGNMENT);
			GridBagConstraints gbc_lblAsunto = new GridBagConstraints();
			gbc_lblAsunto.anchor = GridBagConstraints.EAST;
			gbc_lblAsunto.insets = new Insets(0, 0, 5, 5);
			gbc_lblAsunto.gridx = 0;
			gbc_lblAsunto.gridy = 1;
			contentPanel.add(lblAsunto, gbc_lblAsunto);
		}
		{
			textPane = new JTextField();
			GridBagConstraints gbc_textPane = new GridBagConstraints();
			gbc_textPane.insets = new Insets(0, 0, 5, 5);
			gbc_textPane.fill = GridBagConstraints.BOTH;
			gbc_textPane.gridx = 1;
			gbc_textPane.gridy = 1;
			contentPanel.add(textPane, gbc_textPane);
		}
		{
			JScrollPane scrollPane = new JScrollPane();
			GridBagConstraints gbc_scrollPane = new GridBagConstraints();
			gbc_scrollPane.insets = new Insets(0, 0, 0, 5);
			gbc_scrollPane.gridwidth = 2;
			gbc_scrollPane.fill = GridBagConstraints.BOTH;
			gbc_scrollPane.gridx = 0;
			gbc_scrollPane.gridy = 2;
			contentPanel.add(scrollPane, gbc_scrollPane);
			{
				textPaneContenido = new JTextPane();
				scrollPane.setViewportView(textPaneContenido);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Enviar");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						
						TransferGeneral datos=new TransferGeneral();

						datos.setDniUsuario(dniUsuario);
						datos.setDniDestinoNuevo(usuarioId.get(comboBoxPara.getSelectedIndex()));
						datos.setAsuntoNuevo(textPane.getText());
						datos.setContenidoNuevo(textPaneContenido.getText());
						SingletonControlador.getInstancia().evento(Evento.GENERAL_NUEVO_CORREO, datos);
						
						dispose();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			
		}
		actualizarUsuarios();
	}



	private void actualizarUsuarios(){
		comboBoxPara.removeAllItems();
		
		TransferGeneral datos=new TransferGeneral();

		datos.setTipoUsuario(comboBox.getSelectedIndex());
		SingletonControlador.getInstancia().evento(Evento.GENERAL_ACTUALIZAR_PERSONAL, datos);
		usuarioId=datos.getUsuarioId();
		String[] nombreModel= new String[usuarioId.size()];
		for(int i=0;i<usuarioId.size();i++){
			nombreModel[i]= datos.getUsuarioNombre().get(i)+" "+datos.getUsuarioApellido1().get(i)+" "+datos.getUsuarioApellido2().get(i);
		comboBoxPara.setModel(new DefaultComboBoxModel(nombreModel));
		}


	}





}
